﻿using System;
using System.Web.Mvc;
using Diploma.Models;

namespace DiplomaWeb.Controllers
{
    [Authorize]
    public class CheckPointsController : MasterController
    {
        public CheckPointsController(DataManager dataManager) : base(dataManager)
        {
        }

        public ActionResult View(Guid? taskId, Guid? collaborationRoomId)
        {
            CheckPoint checkpoint = DataManager.CheckPointsRepository.GetCheckPoint(taskId.Value);
            GlobalTask globalTask = DataManager.GlobalTasksRepository.GetGlobalTask(taskId.Value);
           
                        
            if (checkpoint == null)
                throw new Exception();

            if (checkpoint is GlobalTask && (LoggedUser is Student || LoggedUser is Lecturer))
            {
                Student student = DataManager.StudentsRepository.GetStudentByCollaborationRoom(collaborationRoomId.Value);
                if(LoggedUser is Student)
                    ViewData["CheckpointCompletion"] = DataManager.GlobalTasksCompletionsRepository.GetCompletion(globalTask, (Student)LoggedUser);
                if (LoggedUser is Lecturer)
                {
                    ViewData["Student"] = student;
                    ViewData["CheckpointCompletion"] = DataManager.GlobalTasksCompletionsRepository.GetCompletion(globalTask, student);
                }
                GlobalTasksCompletion gtCompletion = DataManager.GlobalTasksCompletionsRepository.GetGlobalTaskCompletion(globalTask,
                                                                                                    student);
                if (gtCompletion != null)
                {
                    ViewData["CompletionDate"] = gtCompletion.CompletionDate;
                }

            }
            else if (checkpoint is Task && (LoggedUser is Student|| LoggedUser is Lecturer))
            {
                Task task = DataManager.TasksRepository.GetTask(taskId.Value);
                ViewData["CheckpointCompletion"] = task.IsCompleted;
                ViewData["CompletionDate"] = task.CompletionDate;
            }
            ViewData.Model = checkpoint;
            ViewData["LoggedUser"] = LoggedUser;           

            return PartialView("~/Views/CheckPoints/CheckPointViewControl.ascx");
        }


        //public ActionResult Delete(Guid id)
        //{
        //    CheckPoint checkpoint = DataManager.CheckPointsRepository.GetCheckPoint(id);
        //    if (checkpoint == null)
        //        throw new Exception();
        //    DataManager.CheckPointsRepository.Delete(checkpoint);
        //    return new EmptyResult();
        //}
    }
}